
#include "file_music.h"

#define MUSIC_SEARCH_PATH  	  ("/home/Videos")

static  pthread_t  music_list_thread;

static  FileListHandle music_file_list = NULL;

static  int  music_total_num     = 0;
static  int  music_search_flag   = 0;


int file_music_get_total(void)
{
	return music_total_num;
}


int file_music_get_fname(char * filename,int index)
{
	int ret = -1;
	
	FileInfo    fileinfo;
	pFileList   filelist ;

	if(!filename)
		return -1;

	if(!music_file_list)
		return -1;

	if(!music_total_num)
		return -1;

	ret = file_list_get_file(music_file_list,index,&fileinfo);
	if(ret != 0){
		return -1;
	}

	file_get_fullname(filename,fileinfo.path,fileinfo.name);

	log_i("i:t:n=%d:%d:%s\n",index,music_total_num,filename);

	return 0;
}


void* file_music_list_task(void *arg)
{
	pFileList	filelist;

	while(1)
	{
		if(!music_search_flag)
		{
			music_search_flag = 1;
			
			file_list_read_all_file(music_file_list,MUSIC_SEARCH_PATH,0);

			filelist = (pFileList)(music_file_list);

			log_i("music num = %d\n",filelist->filenum);

			music_total_num = filelist->filenum;
		}
		else
		{
			usleep(1000);
		}
	}
}

int file_music_list_init(void)
{
	int ret  = 0;

	music_file_list = file_list_create(MUSIC_FILE_LIST);
	if(!music_file_list)
		return -1;

	ret=pthread_create(&music_list_thread, NULL, file_music_list_task, NULL);
	if(ret != 0){
		log_e("thread error=%s\n",strerror(ret));
		return -1;
	}

	return 0;
}


int file_music_list_deinit(void)
{
	if(music_file_list){
		file_list_delete(music_file_list);
		music_file_list = NULL;
	}


	return 0;
}


